//Start functions of Class
var ClassTemplate = "templates/Class.xsl"
var qtSuperClass = 0
function loadClassData(xID){
	if(objectHTTPXML) {
		xIDTmp = xID
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.load.class?id=' + xID + '&m_id=' + modelID , true)
		objectHTTPXML.onreadystatechange = returnClassData
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnClassData(){
	if (objectHTTPXML.readyState == 4) {
		xml = objectHTTPXML.responseXML
		template = loadXSLfromXML(ClassTemplate, xml)
		document.getElementById("divBody").innerHTML = template
		waitBox.hideBox(null);
	}			
}

function saveClass(xID){
	if(objectHTTPXML) {
		var parameters = "m_id=" + modelID +
						 "&id=" + encodeURI(document.getElementById("id").value) +
						 "&name=" + encodeURI(document.getElementById("name").value)
		var superClass = document.getElementsByName("superClass")
		
		if(superClass != null){
			for(var i = 0; i < superClass.length; ++i){
				parameters += "&superClass=" + encodeURI(superClass[i].value)				
			}
		}
		
		if(document.getElementById("isAbstract").checked){
			parameters += "&isAbstract=" + 1
		}
		
		xIDTmp = xID
		
		waitBox = new Lightbox.base('box1');

		objectHTTPXML.onreadystatechange = returnSaveClass
		objectHTTPXML.open('POST', '/model.save.class', true);
		objectHTTPXML.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		objectHTTPXML.setRequestHeader("Content-length", parameters.length);
		objectHTTPXML.setRequestHeader("Connection", "close");
		objectHTTPXML.send(parameters);
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnSaveClass(){
	if (objectHTTPXML.readyState == 4) {
		waitBox.hideBox(null);

		if(objectHTTPXML.responseText == 'OK'){
			changeName(xIDTmp, document.getElementById("name").value)
			alert('Data saved!')
		} else {
			alert('Data not saved!')
			loadClassData(xIDTmp)
		}
	}			
}

function createClass(xID){
	if(objectHTTPXML) {
		xIDTmp = xID
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.create.class?&m_id=' + modelID + '&p_id=' + xID , true)
		objectHTTPXML.onreadystatechange = returnClass
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnClass(){
	if (objectHTTPXML.readyState == 4) {
		if(objectHTTPXML.responseText.split("$$$$").length != 0){
			id = objectHTTPXML.responseText.split("$$$$")
			createNewNode(xIDTmp, id[0], id[1], 'javascript:loadClassData("' + id[0] + '");', 'class.gif', 'class.gif')
		} else {
			alert('Error to create the Class!')
		}
		waitBox.hideBox(null);
	}			
}

function insertSuperClass(id){
	var parameters = Array()

	parameters[0] = window
	parameters[1] = modelID
	parameters[2] = id

	var sc = Array()	
	var superClass = document.getElementsByName("superClass")
	if(superClass != null){
		for(var i = 0; i < superClass.length; ++i){
			sc[i] = superClass[i].value				
		}
	}
	
	parameters[3] = sc
	
	showModalDialog("/templates/getSuperClass.html",parameters,"toolbar:false,status:false;dialogWidth:300px;dialogHeight:150px");
}

function insertSuperClassAux(id, name){
	div = document.getElementById('divSuperClass')
	
	hidden = document.createElement("input")
	hidden.type = "hidden"
	hidden.name = "superClass"
	hidden.value = id

	text1 = document.createTextNode(name + " - ")

	a = document.createElement("a")
	a.href="javascript:deleteSuperClass('" + "superClass" + qtSuperClass + "')"
	a.innerHTML = "Delete"
	
	div2 = document.createElement("div")
	div2.id = "superClass" + qtSuperClass
	div2.appendChild(hidden)
	div2.appendChild(text1)
	div2.appendChild(a)

	div.appendChild(div2)

	qtSuperClass++
	
}

function deleteSuperClass(id){
	div = document.getElementById('divSuperClass')
	div2 = document.getElementById(id)
	div.removeChild(div2)
	qtSuperClass--
}
//End functions of Class